﻿Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms

Public Class DeleteOptionsPage
    Inherits UserControl
    Implements Outlook.PropertyPage

    Private CloseItemImage As Bitmap
    Private _PropertyPageSite As Outlook.PropertyPageSite

    Public Sub New()
        InitializeComponent()
        Me.pbIcon.Image = New Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("DeleteEmails.Delete.png"))
        CloseItemImage = New Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("DeleteEmails.Remove.png"))
    End Sub

    Public Sub Apply() Implements Microsoft.Office.Interop.Outlook.PropertyPage.Apply
        Dim currentitems() As FolderDeleteOptions = (From i As Control In Me.Controls
                                                     Where i.GetType() Is GetType(FolderDeleteOptions)
                                                     Select CType(i, FolderDeleteOptions)).ToArray()
        Dim name As String
        For Each i As RegistryStoredFolderValues In ThisAddIn.FolderValues.ToArray()
            name = i.Name
            If Not Array.Find(currentitems, Function(item As FolderDeleteOptions)
                                                Return item.KeyName = name
                                            End Function) Is Nothing Then ThisAddIn.FolderValues.Remove(i)
        Next

        For Each i As FolderDeleteOptions In currentitems
            ThisAddIn.FolderValues.Add(New RegistryStoredFolderValues(i))
        Next
    End Sub

    Public ReadOnly Property Dirty As Boolean Implements Microsoft.Office.Interop.Outlook.PropertyPage.Dirty
        Get
            For Each i As FolderDeleteOptions In (From c As Control In Me.Controls
                                            Where c.GetType() Is GetType(FolderDeleteOptions)
                                            Select CType(c, FolderDeleteOptions))
                If Not ThisAddIn.FolderValues.Contains(i) Then Return True
                If ThisAddIn.FolderValues.FindByName(i.KeyName) <> i Then Return True
            Next
            Dim name As String
            For Each i As RegistryStoredFolderValues In ThisAddIn.FolderValues
                name = i.Name
                If (From c As Control In Me.Controls
                    Where c.GetType() Is GetType(FolderDeleteOptions) _
                    AndAlso CType(c, FolderDeleteOptions).KeyName = name
                    Select 1).Count() = 0 Then Return True
            Next
            Return False
        End Get
    End Property

    Public Sub GetPageInfo(ByRef HelpFile As String, ByRef HelpContext As Integer) Implements Microsoft.Office.Interop.Outlook.PropertyPage.GetPageInfo
        HelpFile = ""
        HelpContext = 0
    End Sub

    Private Sub Folders_Close(ByVal sender As FolderDeleteOptions)
        Me.Controls.Remove(sender)
        RemoveHandler sender.Close, AddressOf Folders_Close
        sender.Dispose()
        ArrangeFolders()
        PropertyPageSite.OnStatusChange()
    End Sub

    Private Sub Folders_Change(ByVal sender As FolderDeleteOptions)
        PropertyPageSite.OnStatusChange()
    End Sub

    Private Sub ArrangeFolders()
        Dim top As Integer = Me.pbIcon.Bottom + 10
        For Each i As FolderDeleteOptions In (From c As Control In Me.Controls
                                            Where c.GetType() Is GetType(FolderDeleteOptions)
                                            Select CType(c, FolderDeleteOptions))
            i.Top = top
            top = i.Bottom + 2
        Next
        btnAdd.Top = top
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim NewFolder As New FolderDeleteOptions(CloseItemImage)
        Me.Controls.Add(NewFolder)
        NewFolder.Show()
        NewFolder.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        NewFolder.Width = Me.Width - 4
        NewFolder.Left = 2
        AddHandler NewFolder.Close, AddressOf Folders_Close
        AddHandler NewFolder.Change, AddressOf Folders_Change
        ArrangeFolders()
        PropertyPageSite.OnStatusChange()
    End Sub

    Private Function GetPropertyPageSite() As Outlook.PropertyPageSite
        Dim type__1 As Type = GetType(System.Object)
        Dim assembly As String = type__1.Assembly.CodeBase.Replace("mscorlib.dll", "System.Windows.Forms.dll")
        assembly = assembly.Replace("file:///", "")

        Dim assemblyName As String = System.Reflection.AssemblyName.GetAssemblyName(assembly).FullName
        Dim unsafeNativeMethods As Type = Type.[GetType](System.Reflection.Assembly.CreateQualifiedName(assemblyName, "System.Windows.Forms.UnsafeNativeMethods"))

        Dim oleObj As Type = unsafeNativeMethods.GetNestedType("IOleObject")
        Dim methodInfo As System.Reflection.MethodInfo = oleObj.GetMethod("GetClientSite")
        Dim propertyPageSite As Object = methodInfo.Invoke(Me, Nothing)

        Return DirectCast(propertyPageSite, Outlook.PropertyPageSite)
    End Function

    Private ReadOnly Property PropertyPageSite As Outlook.PropertyPageSite
        Get
            If _PropertyPageSite Is Nothing Then _PropertyPageSite = GetPropertyPageSite()
            Return _PropertyPageSite
        End Get
    End Property

    Private Sub DeleteOptionsPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim NewFolder As FolderDeleteOptions
        For Each item As RegistryStoredFolderValues In ThisAddIn.FolderValues
            NewFolder = New FolderDeleteOptions(CloseItemImage, item)
            Me.Controls.Add(NewFolder)
            NewFolder.Show()
            NewFolder.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
            NewFolder.Width = Me.Width - 4
            NewFolder.Left = 2
            AddHandler NewFolder.Close, AddressOf Folders_Close
            AddHandler NewFolder.Change, AddressOf Folders_Change
        Next
        ArrangeFolders()
    End Sub
End Class
